<template>
  <div>
    <el-dialog :title="info.isAdd ? '商品分类添加' : '商品分类修改'" :visible.sync="info.isshow" @close="close">
      <el-form :model="user">
        <el-form-item label="上级分类" label-width="120px">
          <!-- 下拉的菜单-->
          <el-select v-model="user.pid" placeholder="请选择上级菜单">
            <el-option label="顶级分类" :value="0"></el-option>
            <el-option
              v-for="item in showArr"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类名称" label-width="120px">
            <el-input v-model="user.catename"></el-input>
        </el-form-item>
        
       <el-form-item label="图片" label-width="120px">
          <!-- 3.图片上传功能-->
          
          <el-upload
            class="avatar-uploader"
            action="https://jsonplaceholder.typicode.com/posts/"
            :show-file-list="true"
            :before-upload="beforeAvatarUpload"
           
           
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="user.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add">添 加</el-button>
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { formClose } from "../../../mixins";
import { successAlert,errorAlert } from "../../../utils/alert";
import { reqcateadd, reqcateedit, reqcateinfo } from "../../../http/api";
import path from "path";



export default {
  props: ["info"],
  mixins: [formClose],
  data() {
    return {

      user: {
       
        pid: "",
        catename: "",
        img: null,
        status:1,
        
      },
      //图片上传
      imageUrl:'',
   
    };
  },
  computed: {
    ...mapGetters({
      arr:'cate/arr'
    }),
    showArr() {
      return this.arr.filter((item) => item.id !== this.user.id);
    },
  },
  mounted() {},
  methods: {
    ...mapActions({
      reqArr:'cate/reqArr'
    }),

    //
    empty() {
      this.user = {
      
        pid: "",
        catename: "",
        img: null,
        status:1,
         
      };
      this. imageUrl='';
    },
     // 5.4 点了添加
    add() {
      // console.log('@@@@',111);
      
      reqcateadd(this.user).then((res) => {
        if (res.data.code == 200) {
          // ①弹成功
          successAlert(res.data.msg);
          // ②弹框内容清空
          this.empty();
          // ③弹框消失
          this.info.isshow = false;
          // ④重新请求列表
          // this.$emit("init");
          this.reqArr()

        }
      });
    },
        // 8. 获取一条数据
    getOne(id) {
      reqcateinfo({ id: id }).then((res) => {
        if (res.data.code === 200) {
          this.user = res.data.list;
          //处理图片
          this.imageUrl = this.$pre + this.user.img;
          // 补id
          this.user.id = id;
        }
      });
    },
     // 9.修改
    update() {
      reqcateedit(this.user).then((res) => {
        if (res.data.code == 200) {
          // ①弹成功
          successAlert(res.data.msg);
          // ②弹框内容清空
          this.empty();
          // ③弹框消失
          this.info.isshow = false;
          // ④重新请求列表
          // this.$emit("init");
              this.reqArr()
        }
      });
    },
     beforeAvatarUpload(file){
    //   let file=e.target.files[0];//文件
    
      if(file.size>2*1024*1024){
        errorAlert("文件最大2M")
        return;
      }

      //验证文件格式
      let imgArr=['.png','.jpeg','.jpg','.gif','.webp'];  
      let extname=path.extname(file.name)// .png
      if(!imgArr.includes(extname)){
        errorAlert("请上传正确的图片格式")
        return;
      }
      //生成图片地址
      this.imageUrl=URL.createObjectURL(file)

      //存到user，后续发送给后端
      this.user.img=file;
    }
  }
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>